package com.etang.a2020b1019.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bizideal.smarthome.socket.ConstantUtil;
import com.bizideal.smarthome.socket.ControlUtils;
import com.etang.a2020b1019.R;

/**
 * @author Administrator
 */
public class ControlActivity extends Fragment {
    private Button btn_dvd;
    private Button btn_kt;
    private Button btn_tv;
    private Button btn_door;
    private Button btn_open;
    private Button btn_cls;
    private Button btn_stop;
    private ToggleButton tg_warm;
    private ToggleButton tg_fan;
    private ToggleButton tg_lamp;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_control, null, false);
        initView(view);
        tg_fan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub

                if (isChecked) {
                    ControlUtils.control(ConstantUtil.Fan,
                            ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.Fan,
                            ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                }
            }
        });
        tg_lamp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub

                if (isChecked) {
                    ControlUtils.control(ConstantUtil.Lamp,
                            ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.Lamp,
                            ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                }
            }
        });
        tg_warm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub

                if (isChecked) {
                    ControlUtils.control(ConstantUtil.WarningLight,
                            ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.WarningLight,
                            ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                }
            }
        });
        btn_cls.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ControlUtils.control(ConstantUtil.Curtain,
                        ConstantUtil.CHANNEL_1, ConstantUtil.CLOSE);
            }
        });
        btn_open.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ControlUtils.control(ConstantUtil.Curtain,
                        ConstantUtil.CHANNEL_3, ConstantUtil.OPEN);
            }
        });
        btn_stop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ControlUtils.control(ConstantUtil.Curtain,
                        ConstantUtil.CHANNEL_2, ConstantUtil.CLOSE);
            }
        });
        btn_door.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ControlUtils.control(ConstantUtil.RFID_Open_Door,
                        ConstantUtil.CHANNEL_1, ConstantUtil.OPEN);
            }
        });
        btn_dvd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "8",
                        ConstantUtil.OPEN);
            }
        });
        btn_kt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "1",
                        ConstantUtil.OPEN);
            }
        });
        btn_tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "5",
                        ConstantUtil.OPEN);
            }
        });
        return view;
    }

    private void initView(View view) {
        // TODO Auto-generated method stub\
        btn_cls = (Button) view.findViewById(R.id.btn_cls);
        btn_open = (Button) view.findViewById(R.id.btn_open);
        btn_stop = (Button) view.findViewById(R.id.btn_stop);
        tg_fan = (ToggleButton) view.findViewById(R.id.tg_fan);
        tg_lamp = (ToggleButton) view.findViewById(R.id.tg_lamp);
        tg_warm = (ToggleButton) view.findViewById(R.id.tg_warm);
        btn_door = (Button) view.findViewById(R.id.btn_door);
        btn_tv = (Button) view.findViewById(R.id.btn_tv);
        btn_dvd = (Button) view.findViewById(R.id.btn_dvd);
        btn_kt = (Button) view.findViewById(R.id.btn_kt);
    }

}
